
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "client.h"

void init_display()
{
	int type;
	int flag = 1;
	while(flag){
		printf("******************************************\n");
		printf("*****   1:登录    2:注册    3:退出   *****\n");
		printf("******************************************\n");
		printf("*****   请选择:");
    
		scanf("%d",&type);
		while(!('\n' ==getchar()));
		switch(type){
		case 1:
			if(ERROR==do_login()){
				printf("请重新登录 !\n");
			}
			break; 
		case 2:
			if(ERROR ==do_register()){
				printf("请重新注册 !\n");
			}
			break;
		case 3:
			flag = 0;
			break; 
		}
	}
}


ER_TYPE  do_login()
{
	
	int choice; 
	MSG_T user_msg;
	
	printf("1:普通用户登录 2:管理员登录 3:返回\n");
	printf("请选择：");
    scanf("%d",&choice); 
	while(!('\n' ==getchar()));
	if((choice ==1) || (choice ==2)){
		printf("Please input username:");
		fgets(user_msg.user_info.name,sizeof(user_msg.user_info.name),stdin);	
		user_msg.user_info.name[strlen(user_msg.user_info.name)-1] ='\0';
			
		printf("Please input password:");
		fgets(user_msg.user_info.password,sizeof(user_msg.user_info.password),stdin);	
		user_msg.user_info.password[strlen(user_msg.user_info.password)-1] ='\0';
		
		if(choice ==1){
			user_msg.usertype = USER ; 
		}else{
			user_msg.usertype = ROOT ; 
		}

		do_send(&user_msg);   //向服务器端发送用户名及密码 
		do_recv(&user_msg);   //需要服务器端回填 请求结果码
		if(user_msg.errornum == OK){
			printf("User:%s login success!\n",user_msg.user_info.name);
		}else{
			printf("Login failed !\n");
			return ERROR; 
		}
	}
	switch(choice){
		case 1:
			user_login(user_msg);
			break;
		case 2:
			root_login(user_msg);
			break;
		default:
			break;
	}
	return OK;
}


//登录成功后进入用户登录选择界面
void user_login(MSG_T user_msg)
{
	
	int choice;
	int flag = 1; //循环结束标志
	while(flag){ 
		printf("1:查找 2:修改 3:返回\n");
		printf("请选择：");
    	scanf("%d",&choice);
		while(!('\n' ==getchar()));
		switch(choice){
			case 1:
				if(ERROR==user_query(user_msg)){
					continue ;		
				}
				break;
			case 2:
				if(ERROR==user_update(user_msg)){
					continue;
				}
				break; 
			default:
				flag = 0; //结束while循环
				break;
		}
	}
}


/*普通用户查询函数：
 * 目前只能跟据用户名进行查询
 * user_msg结构体中需要填充:用户名 
 * */
ER_TYPE user_query(MSG_T user_msg)
{
	user_msg.flag = QUERY;
	do_send(&user_msg); //向服务器发送查寻请求
	memset(&user_msg,0,sizeof(user_msg));
	do_recv(&user_msg); //等待服务器回传消息 

	if(user_msg.errornum == OK){
		printf("工  号：%d\n",user_msg.user_info.num);
		printf("用户名：%s\n",user_msg.user_info.name);
	//	printf("密  码：%s\n",user_msg.user_info.password);
		printf("电  话：%s\n",user_msg.user_info.telphone);
		printf("地  址：%s\n",user_msg.user_info.address);
	}else{
		printf("查寻失败!\n");
	}
	return user_msg.errornum ; 
}


/*普通用户不能更新用户名及工号
 * 具体更改项目：
 * 			 <1> 密码 
 *           <2> 年龄
 *           <3> 电话
 *           <4> 地址 
 * */
ER_TYPE  user_update(MSG_T user_msg)
{
	printf("请输入最新的个人信息:\n");
	printf("新密码:");
	fgets(user_msg.user_info.password,sizeof(user_msg.user_info.password),stdin);	
	user_msg.user_info.password[strlen(user_msg.user_info.password)-1] ='\0';
	printf("新年龄:");
	scanf("%d",&(user_msg.user_info.age));	
	while(!('\n' ==getchar()));
	printf("新电话:");
	fgets(user_msg.user_info.telphone,sizeof(user_msg.user_info.telphone),stdin);	
	user_msg.user_info.telphone[strlen(user_msg.user_info.telphone)-1] ='\0';
	printf("新地址:");
	fgets(user_msg.user_info.address,sizeof(user_msg.user_info.address),stdin);	
	user_msg.user_info.address[strlen(user_msg.user_info.address)-1] ='\0';
	user_msg.flag = UPDATE; 
	do_send(&user_msg);
	memset(&user_msg,0,sizeof(user_msg));
	do_recv(&user_msg); //等待服务器回传消息 
	if(user_msg.errornum ==OK){
		printf("更新成功!\n");
	}else{
		printf("更新失败!\n");
	}

	return ERROR;
}


//登录成功后进入 管理员 登录选择界面
void root_login(MSG_T user_msg)
{ 	
	int choice; 
	int flag = 1; 
	while(flag){
		printf("1:增加 2:删除 3:修改 4:查找 5:返回\n");
		printf("请选择：");
    	scanf("%d",&choice);
		while(!('\n' ==getchar()));
		//user_msg.usertype = USER;
		switch(choice){
			case 1:
				root_add(user_msg);
				break;
			case 2:
				root_delete(user_msg);
				break;
			case 3:
				root_update(user_msg);
				break;
			case 4:
				user_msg.flag = QUERY;
				root_query(user_msg);
				break;
			default:
				flag = 0;
				break;
		}
	}
}

/*添加成功返回：OK, 失败返回：ERROR */ 
ER_TYPE root_add(MSG_T user_msg)
{
 	
	printf("请输入要添加的个人信息:\n");
	printf("用户名:");
	fgets(user_msg.user_info.name,sizeof(user_msg.user_info.name),stdin);	
	user_msg.user_info.name[strlen(user_msg.user_info.name)-1] ='\0';
	printf("密  码:");
	fgets(user_msg.user_info.password,sizeof(user_msg.user_info.password),stdin);	
	user_msg.user_info.password[strlen(user_msg.user_info.password)-1] ='\0';
	printf("年  龄:");
	scanf("%d",&(user_msg.user_info.age));	
	while(!('\n' ==getchar()));
	printf("电  话:");
	fgets(user_msg.user_info.telphone,sizeof(user_msg.user_info.telphone),stdin);	
	user_msg.user_info.telphone[strlen(user_msg.user_info.telphone)-1] ='\0';
	printf("地  址:");
	fgets(user_msg.user_info.address,sizeof(user_msg.user_info.address),stdin);	
	user_msg.user_info.address[strlen(user_msg.user_info.address)-1] ='\0';
	user_msg.flag = ADD ; 
	do_send(&user_msg);
	memset(&user_msg,0,sizeof(user_msg));
	do_recv(&user_msg); //等待服务器回传消(需要服务器端填充 工号 )

	if(user_msg.errornum ==OK){
		printf("添加成功!\n");
	//	printf("新添加成员的工号为：%d\n",user_msg.user_info.num);
	}else{
		printf("添加失败!\n");
	}
	return user_msg.errornum;
}

ER_TYPE root_delete(MSG_T user_msg)
{
	printf("请输入你要删除的用户名:");
	fgets(user_msg.user_info.name,sizeof(user_msg.user_info.name),stdin);	
	user_msg.user_info.name[strlen(user_msg.user_info.name)-1] ='\0';
	
	user_msg.flag = DELETE ; 
	do_send(&user_msg);
	memset(&user_msg,0,sizeof(user_msg));
	do_recv(&user_msg); //等待服务器回传消息 
	if(user_msg.errornum ==OK){
		printf("删除成功！\n");
	}else{
		printf("删除失败!\n");
	}
	return user_msg.errornum;
}

/*管理员 根据用户名来修改用户信息
 * 具体更改项目：
 * 			 <1> 密码 
 *           <2> 年龄
 *           <3> 电话
 *           <4> 地址 
 * */
ER_TYPE  root_update(MSG_T user_msg)
{
	printf("请输入要更改的用户名信息:");
	fgets(user_msg.user_info.name,sizeof(user_msg.user_info.name),stdin);	
	user_msg.user_info.name[strlen(user_msg.user_info.name)-1] ='\0';
		
	printf("请输入最新的个人信息:\n");
	printf("新年龄:");
	scanf("%d",&(user_msg.user_info.age));	
	while(!('\n' ==getchar()));
	printf("新电话:");
	fgets(user_msg.user_info.telphone,sizeof(user_msg.user_info.telphone),stdin);	
	user_msg.user_info.telphone[strlen(user_msg.user_info.telphone)-1] ='\0';
	printf("新地址:");
	fgets(user_msg.user_info.address,sizeof(user_msg.user_info.address),stdin);	
	user_msg.user_info.address[strlen(user_msg.user_info.address)-1] ='\0';
	user_msg.flag = UPDATE; 
	do_send(&user_msg);
	memset(&user_msg,0,sizeof(user_msg));
	do_recv(&user_msg); //等待服务器回传消息 
	if(user_msg.errornum ==OK){
		printf("更新成功!\n");
	}else{
		printf("更新失败!\n");
	}

	return ERROR;
}


ER_TYPE root_query(MSG_T user_msg)
{
	user_msg.flag = QUERY;
	do_send(&user_msg);

	do{
	 	do_recv(&user_msg);
		if(user_msg.errornum == OK){
			printf("工  号：%d\n",user_msg.user_info.num);
			printf("用户名：%s\n",user_msg.user_info.name);
			printf("年  龄: %d\n",user_msg.user_info.age);
			printf("电  话：%s\n",user_msg.user_info.telphone);
			printf("地  址：%s\n",user_msg.user_info.address);
		}else{
			printf("查寻完毕!\n");
		}
	}while(user_msg.errornum ==OK);
}



ER_TYPE  do_register()
{
	MSG_T user_msg;

		printf("Please input username:");
		fgets(user_msg.user_info.name,sizeof(user_msg.user_info.name),stdin);	
		user_msg.user_info.name[strlen(user_msg.user_info.name)-1] ='\0';
			
		printf("Please input password:");
		fgets(user_msg.user_info.password,sizeof(user_msg.user_info.password),stdin);	
		user_msg.user_info.password[strlen(user_msg.user_info.password)-1] ='\0';
	
	user_msg.usertype = REGISTER; //告诉服务器是register请求

	do_send(&user_msg); 
	memset(&user_msg,0,sizeof(user_msg)); 
	do_recv(&user_msg); //注册成功后需要服务器端发送一个成功标志
	if(user_msg.errornum == OK){
		printf("User:%s register success!\n",user_msg.user_info.name);
	}else{
	    printf("User:%s register fialed!\n",user_msg.user_info.password);
		
	} 
	return user_msg.errornum;
}



